Skip to content

Commit

Permalink
Edit activity types interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpchi committed May 31, 2024
1 parent 29393a1 commit 462ed94
Show file tree
Hide file tree
Showing 17 changed files with 605 additions and 5 deletions.
2 changes: 2 additions & 0 deletions activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@
echo $OUTPUT->render($notify);

$activityform->display();
$activitytypes = $DB->get_records_menu('local_apprenticeactivities', [], '', 'id,description');
$PAGE->requires->js_call_amd('local_apprenticeoffjob/activitytypes', 'init', [$activitytypes]);
echo $OUTPUT->footer();
3 changes: 3 additions & 0 deletions amd/build/activitytypes.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions amd/build/activitytypes.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions amd/src/activitytypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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/>.

/**
* Add dynamic help when adding or editing an activity.
*
* @module local_apprenticeoffjob/activitytypes
* @copyright 2024 Solent University {@link https://www.solent.ac.uk}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

export const init = (activitytypes) => {
const activitytypediv = document.querySelector('#id_error_activitytype');
if (!activitytypediv) {
return;
}
let activitydesc = document.createElement('div');
activitydesc.setAttribute('id', 'activitydesc');
activitydesc.style.width = '100%';
activitydesc.style.marginTop = '10px';
activitytypediv.after(activitydesc);
let activity = document.querySelector('#id_activitytype').value;
if (activity > 0) {
activitydesc.innerHTML = activitytypes[activity] ?? '';
}
document.addEventListener('change', e => {
if (e.target.id == 'id_activitytype') {
activity = e.target.value;
if (activity > 0) {
activitydesc.innerHTML = activitytypes[activity] ?? '';
}
}
});
};
54 changes: 54 additions & 0 deletions classes/activitytype.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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/>.

namespace local_apprenticeoffjob;

use core\persistent;

/**
* Class activitytype
*
* @package local_apprenticeoffjob
* @copyright 2024 Solent University {@link https://www.solent.ac.uk}
* @author Mark Sharp <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class activitytype extends persistent {
/**
* Table name for activity types
*/
const TABLE = 'local_apprenticeactivities';

/**
* Define properties for model
*
* @return array
*/
protected static function define_properties(): array {
return [
'activityname' => [
'type' => PARAM_TEXT,
],
'description' => [
'type' => PARAM_RAW,
],
'status' => [
'type' => PARAM_BOOL,
'default' => false,
],
];
}
}
20 changes: 20 additions & 0 deletions classes/forms/activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function definition() {

$activitytypes = api::get_activitytypes();
$activityoptions = [];
$activityoptions[] = 'Please select';
foreach ($activitytypes as $type) {
$activityoptions[$type->id] = $type->activityname;
}
Expand All @@ -62,6 +63,9 @@ public function definition() {

$mform->addElement('select', 'activitytype', get_string('activitytype', 'local_apprenticeoffjob'), $activityoptions);
$mform->setType('activitytype', PARAM_INT);
$mform->addRule('activitytype', new lang_string('required'), 'required', null, 'client');
$mform->addRule('activitytype', new lang_string('required'), 'nonzero', null, 'client');
$mform->addRule('activitytype', get_string('errnumeric', 'local_apprenticeoffjob'), 'numeric', null, 'server');

$mform->addElement('date_selector', 'activitydate', get_string('activitydate', 'local_apprenticeoffjob'));
$mform->setType('activitydate', PARAM_INT);
Expand All @@ -87,4 +91,20 @@ public function definition() {

$this->add_action_buttons();
}

/**
* Validate the fields we need to check.
*
* @param array $data
* @param array $files
* @return array Errors
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
$activitytypes = api::get_activitytypes();
if (!isset($activitytypes[$data['activitytype']])) {
$errors['activitytype'] = get_string('invalidactivitytypeid', 'local_apprenticeoffjob');
}
return $errors;
}
}
57 changes: 57 additions & 0 deletions classes/forms/activitytype_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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/>.

namespace local_apprenticeoffjob\forms;

use context_system;
use core\form\persistent as persistent_form;
use lang_string;

/**
* Class activitytype
*
* @package local_apprenticeoffjob
* @copyright 2024 Solent University {@link https://www.solent.ac.uk}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class activitytype_form extends persistent_form {
/**
* Cross reference for the object this form is working from.
*
* @var string
*/
protected static $persistentclass = 'local_apprenticeoffjob\\activitytype';

/**
* Activity type object editing form
*
* @return void
*/
public function definition() {
$mform = $this->_form;
$required = new lang_string('required');
$mform->addElement('text', 'activityname', new lang_string('activityname', 'local_apprenticeoffjob'));
$mform->addRule('activityname', $required, 'required', null, 'client');

$mform->addElement('textarea', 'description', new lang_string('description'), 'wrap="virtual" rows="20" cols="50"');
$mform->setType('description', PARAM_RAW);

$mform->addElement('advcheckbox', 'status', new lang_string('enabled', 'local_apprenticeoffjob'));
$mform->addElement('hidden', 'id');

$this->add_action_buttons();
}
}
126 changes: 126 additions & 0 deletions classes/tables/activitytypes_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?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/>.

namespace local_apprenticeoffjob\tables;

use core_user;
use html_writer;
use lang_string;
use moodle_url;
use table_sql;

/**
* Class activitytypes_table
*
* @package local_apprenticeoffjob
* @copyright 2024 Solent University {@link https://www.solent.ac.uk}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class activitytypes_table extends table_sql {
/**
* Constructor to set up table
*
* @param string $uniqueid
*/
public function __construct($uniqueid) {
parent::__construct($uniqueid);
$this->useridfield = 'modifiedby';
$columns = [
'id',
'activityname',
'description',
'enabled',
'usermodified',
'timemodified',
'actions',
];

$columnheadings = [
'id',
new lang_string('activityname', 'local_apprenticeoffjob'),
new lang_string('description'),
new lang_string('enabled', 'local_apprenticeoffjob'),
new lang_string('modifiedby', 'local_apprenticeoffjob'),
new lang_string('lastmodified', 'local_apprenticeoffjob'),
new lang_string('actions', 'local_apprenticeoffjob'),
];

$this->define_columns($columns);
$this->define_headers($columnheadings);
$this->no_sorting('actions');
$this->sortable(true, 'activityname', SORT_ASC);
$this->collapsible(false);

$this->define_baseurl(new moodle_url("/local/apprenticeoffjob/manageactivitytypes.php"));
$where = '1=1';
$this->set_sql('*', "{local_apprenticeactivities}", $where);
}

/**
* Output actions column
*
* @param stdClass $row
* @return string HTML for row's column value
*/
public function col_actions($row) {
$params = ['action' => 'edit', 'id' => $row->id];
$edit = new moodle_url('/local/apprenticeoffjob/editactivitytype.php', $params);
$html = html_writer::link($edit, get_string('edit'));

return $html;
}

/**
* Output enabled column
*
* @param stdClass $row
* @return string HTML for row's column value
*/
public function col_enabled($row) {
return ($row->status) ? new lang_string('enabled', 'local_apprenticeoffjob')
: new lang_string('notenabled', 'local_apprenticeoffjob');
}

/**
* Output usermodified column
*
* @param stdClass $row
* @return string HTML for row's column value
*/
public function col_usermodified($row) {
if ($row->usermodified == 0) {
return '';
}
$modifiedby = core_user::get_user($row->usermodified);
if (!$modifiedby || $modifiedby->deleted) {
return get_string('deleteduser', 'local_apprenticeoffjob');
}
return fullname($modifiedby);
}

/**
* Output timemodified column
*
* @param stdClass $row
* @return string HTML for row's column value
*/
public function col_timemodified($row) {
if ($row->timemodified == 0) {
return '';
}
return userdate($row->timemodified, get_string('strftimedatetimeshort', 'core_langconfig'));
}
}
20 changes: 20 additions & 0 deletions db/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,46 @@
*/
function xmldb_local_apprenticeoffjob_install() {
global $CFG, $DB;
$user = get_admin();
$activities = [];
$dataobject1 = new stdClass();
$dataobject1->activityname = 'Teaching of Theory';
$dataobject1->status = 1;
$dataobject1->usermodified = $user->id;
$dataobject1->timemodified = time();
$dataobject1->timecreated = time();
$activities[] = $dataobject1;

$dataobject2 = new stdClass();
$dataobject2->activityname = 'Practical Training';
$dataobject2->status = 1;
$dataobject2->usermodified = $user->id;
$dataobject2->timemodified = time();
$dataobject2->timecreated = time();
$activities[] = $dataobject2;

$dataobject3 = new stdClass();
$dataobject3->activityname = 'Assignments, Projects & Portfolio (SDS)';
$dataobject3->status = 1;
$dataobject3->usermodified = $user->id;
$dataobject3->timemodified = time();
$dataobject3->timecreated = time();
$activities[] = $dataobject3;

$dataobject4 = new stdClass();
$dataobject4->activityname = 'Work Shadowing';
$dataobject4->status = 1;
$dataobject4->usermodified = $user->id;
$dataobject4->timemodified = time();
$dataobject4->timecreated = time();

$activities[] = $dataobject4;
$dataobject5 = new stdClass();
$dataobject5->activityname = 'Mentoring';
$dataobject5->status = 1;
$dataobject5->usermodified = $user->id;
$dataobject5->timemodified = time();
$dataobject5->timecreated = time();
$activities[] = $dataobject5;

$DB->insert_records('local_apprenticeactivities', $activities);
Expand Down
Loading

0 comments on commit 462ed94

Please sign in to comment.