Skip to content

Commit

Permalink
#222 #234 DRAFT - Refactoring to simplify codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastsg committed Oct 15, 2024
1 parent 2b45097 commit 71e4efa
Show file tree
Hide file tree
Showing 97 changed files with 1,873 additions and 5,811 deletions.
26 changes: 16 additions & 10 deletions action.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,34 @@
*
* @package mod_capquiz
* @author Aleksander Skrede <[email protected]>
* @copyright 2018 NTNU
* @copyright 2018 Norwegian University of Science and Technology (NTNU)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_capquiz;
use mod_capquiz\capquiz;
use mod_capquiz\capquiz_action_performer;

require_once ("../../config.php");
global $CFG;

require_once("../../config.php");
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->dirroot . '/mod/capquiz/lib.php');
require_once($CFG->dirroot . '/mod/capquiz/classes/capquiz_action_performer.php');

$cmid = capquiz_urls::require_course_module_id_param();
global $PAGE;

$cmid = required_param('id', PARAM_INT);
$cm = get_coursemodule_from_id('capquiz', $cmid, 0, false, MUST_EXIST);
require_login($cm->course, false, $cm);

$context = \context_module::instance($cmid);
$context = context_module::instance($cmid);
require_capability('mod/capquiz:instructor', $context);

$action = required_param('action', PARAM_TEXT);

$capquiz = new capquiz($cmid);
capquiz_urls::set_page_url($capquiz, capquiz_urls::$urlasync);
capquiz_action_performer::perform($action, $capquiz);
$PAGE->set_context($context);
$PAGE->set_cm($cm);
$PAGE->set_url(new moodle_url('/mod/capquiz/async.php'));

capquiz_action_performer::perform($action, new capquiz($cmid));

capquiz_urls::redirect_to_dashboard();
redirect(new moodle_url('/mod/capquiz/view.php', ['id' => $cmid]));
2 changes: 2 additions & 0 deletions adminmanageplugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

require_once("../../config.php");

global $CFG, $PAGE;

require_login();

require_once($CFG->dirroot . '/mod/capquiz/adminlib.php');
Expand Down
32 changes: 19 additions & 13 deletions async.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,42 @@
*
* @package mod_capquiz
* @author Aleksander Skrede <[email protected]>
* @copyright 2018 NTNU
* @copyright 2018 Norwegian University of Science and Technology (NTNU)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_capquiz;
use mod_capquiz\capquiz;
use mod_capquiz\capquiz_question_attempt;
use mod_capquiz\capquiz_question_engine;

require_once('../../config.php');
require_once(__DIR__ . '/../../config.php');

$cmid = capquiz_urls::require_course_module_id_param();
global $PAGE;

$cmid = required_param('id', PARAM_INT);
$cm = get_coursemodule_from_id('capquiz', $cmid, 0, false, MUST_EXIST);
require_login($cm->course, false, $cm);
$context = \context_module::instance($cmid);

$context = context_module::instance($cmid);
require_capability('mod/capquiz:student', $context);

$action = required_param('action', PARAM_TEXT);
$attemptid = optional_param('attempt', null, PARAM_INT);
$cmid = capquiz_urls::require_course_module_id_param();
$capquiz = new capquiz($cmid);
$action = required_param('action', PARAM_TEXT);

capquiz_urls::set_page_url($capquiz, capquiz_urls::$urlasync);
$PAGE->set_context($context);
$PAGE->set_cm($cm);
$PAGE->set_url(new moodle_url('/mod/capquiz/async.php'));

if ($attemptid !== null) {
$capquiz = new capquiz($cmid);
$user = $capquiz->user();
$attempt = capquiz_question_attempt::load_attempt($user, $attemptid);
if ($action === 'answered') {
$capquiz->question_engine($user)->attempt_answered($user, $attempt);
(new capquiz_question_engine($capquiz))->attempt_answered($user, $attempt);
} else if ($action === 'reviewed') {
$capquiz->question_engine($user)->attempt_reviewed($attempt);
(new capquiz_question_engine($capquiz))->attempt_reviewed($attempt);
}
capquiz_urls::redirect_to_dashboard();
redirect(new moodle_url('/mod/capquiz/view.php', ['id' => $cmid]));
}

capquiz_urls::redirect_to_front_page();
redirect(new moodle_url('/'));
27 changes: 7 additions & 20 deletions backup/moodle2/backup_capquiz_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ protected function define_structure() {
'timemodified',
'published',
'default_user_rating',
'stars_to_pass',
'timedue',
'numquestioncandidates',
'minquestionsuntilreappearance',
'userwinprobability',
'userkfactor',
'questionkfactor',
]);
$questionlist = new backup_nested_element('questionlist', null, [
'id',
Expand All @@ -68,18 +75,6 @@ protected function define_structure() {
'manual',
'timecreated',
]);
$questionselections = new backup_nested_element('questionselections');
$questionselection = new backup_nested_element('questionselection', ['id'], [
'capquiz_id',
'strategy',
'configuration',
]);
$ratingsystems = new backup_nested_element('ratingsystems');
$ratingsystem = new backup_nested_element('ratingsystem', ['id'], [
'capquiz_id',
'rating_system',
'configuration',
]);
$users = new backup_nested_element('users');
$user = new backup_nested_element('user', ['id'], [
'user_id',
Expand Down Expand Up @@ -121,12 +116,6 @@ protected function define_structure() {
$question->add_child($questionratings);
$questionratings->add_child($questionrating);

$capquiz->add_child($questionselections);
$questionselections->add_child($questionselection);

$capquiz->add_child($ratingsystems);
$ratingsystems->add_child($ratingsystem);

$capquiz->add_child($users);
$users->add_child($user);
$user->add_child($userratings);
Expand All @@ -139,8 +128,6 @@ protected function define_structure() {
$questionlist->set_source_table('capquiz_question_list', ['capquiz_id' => backup::VAR_PARENTID]);
$question->set_source_table('capquiz_question', ['question_list_id' => backup::VAR_PARENTID]);
$questionrating->set_source_table('capquiz_question_rating', ['capquiz_question_id' => backup::VAR_PARENTID]);
$questionselection->set_source_table('capquiz_question_selection', ['capquiz_id' => backup::VAR_PARENTID]);
$ratingsystem->set_source_table('capquiz_rating_system', ['capquiz_id' => backup::VAR_PARENTID]);
if ($this->get_setting_value('userinfo')) {
$user->set_source_table('capquiz_user', ['capquiz_id' => backup::VAR_PARENTID]);
$userrating->set_source_table('capquiz_user_rating', ['capquiz_user_id' => backup::VAR_PARENTID]);
Expand Down
33 changes: 1 addition & 32 deletions backup/moodle2/restore_capquiz_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,10 @@ class restore_capquiz_activity_structure_step extends restore_questions_activity
protected function define_structure() {
$paths = [];
$paths[] = new restore_path_element('capquiz', '/activity/capquiz');
$questionlist = new restore_path_element('capquiz_question_list', '/activity/capquiz/questionlist');
$paths[] = $questionlist;
$paths[] = new restore_path_element('capquiz_question_list', '/activity/capquiz/questionlist');
$paths[] = new restore_path_element('capquiz_question', '/activity/capquiz/questionlist/questions/question');
$paths[] = new restore_path_element(
'capquiz_question_rating', '/activity/capquiz/questionlist/questions/question/questionratings/question_rating');
$paths[] = new restore_path_element('capquiz_question_selection', '/activity/capquiz/questionselections/questionselection');
$paths[] = new restore_path_element('capquiz_rating_system', '/activity/capquiz/ratingsystems/ratingsystem');
if ($this->get_setting_value('userinfo')) {
$capuser = new restore_path_element('capquiz_user', '/activity/capquiz/users/user');
$this->add_question_usages($capuser, $paths);
Expand Down Expand Up @@ -124,34 +121,6 @@ protected function process_capquiz_question_rating($data) {
$this->set_mapping('capquiz_question_rating', $oldid, $newitemid);
}

/**
* Processes and backs up capquiz question selection
*
* @param object $data
*/
protected function process_capquiz_question_selection($data) {
global $DB;
$data = (object)$data;
$data->capquiz_id = $this->get_new_parentid('capquiz');
$oldid = $data->id;
$newitemid = $DB->insert_record('capquiz_question_selection', $data);
$this->set_mapping('capquiz_question_selection', $oldid, $newitemid);
}

/**
* Processes and backs up capquiz question rating system
*
* @param object $data
*/
protected function process_capquiz_rating_system($data) {
global $DB;
$data = (object)$data;
$data->capquiz_id = $this->get_new_parentid('capquiz');
$oldid = $data->id;
$newitemid = $DB->insert_record('capquiz_rating_system', $data);
$this->set_mapping('capquiz_rating_system', $oldid, $newitemid);
}

/**
* Processes and backs up capquiz user
*
Expand Down
14 changes: 10 additions & 4 deletions classes/bank/add_to_quiz_action.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

namespace mod_capquiz\bank;

use mod_capquiz\capquiz_urls;
use moodle_url;
use stdClass;

/**
* Question bank action to add question to quiz.
*
* @package mod_capquiz
* @copyright 2024 NTNU
* @author 2024 Sebastian Gundersen
* @author 2024 Sebastian Gundersen <[email protected]>
* @copyright 2024 Norwegian University of Science and Technology (NTNU)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class add_to_quiz_action extends \core_question\local\bank\question_action_base {
Expand All @@ -38,9 +38,15 @@ class add_to_quiz_action extends \core_question\local\bank\question_action_base
* $label - text label to display in the UI (either in the menu, or as a tool-tip on the icon)
*/
protected function get_url_icon_and_label(stdClass $question): array {
global $PAGE;
if (!question_has_capability_on($question, 'use')) {
return [null, null, null];
}
return [capquiz_urls::add_question_to_list_url($question->id), 't/add', get_string('addtoquiz', 'quiz')];
$url = new moodle_url('/mod/capquiz/action.php', [
'id' => $PAGE->cm->id,
'action' => 'add-question',
'question-id' => $question->id,
]);
return [$url, 't/add', get_string('addtoquiz', 'quiz')];
}
}
50 changes: 34 additions & 16 deletions classes/bank/question_bank_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,51 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* This file defines a class represeting a question bank view.
*
* It is based on similar implementations from the Core Quiz,
* but intended to run in a pane rather than a modal overlay,
* some differences are needed. It includes legacy code from
* different versions of moodle, and should have been refactored.
*
* @package mod_capquiz
* @author Hans Georg Schaathun <[email protected]>
* @copyright 2018/2022 NTNU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_capquiz\bank;

use context;

/**
* Class question_bank_view
* Question bank view for CAPQuiz.
*
* @package mod_capquiz
* @author Sebastian Gundersen <[email protected]>
* @author Aleksander Skrede <[email protected]>
* @copyright 2018 NTNU
* @copyright 2024 NTNU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_bank_view extends \core_question\local\bank\view {
/**
* Constructor.
*/
public function __construct() {
global $PAGE;
$PAGE->set_pagelayout('admin');
$params = [
'cmid' => $PAGE->cm->id,
'qpage' => optional_param('qpage', null, PARAM_INT),
'cat' => optional_param('cat', null, PARAM_SEQUENCE),
'category' => optional_param('category', null, PARAM_SEQUENCE),
'qperpage' => optional_param('qperpage', null, PARAM_INT),
'recurse' => optional_param('recurse', null, PARAM_BOOL),
'showhidden' => optional_param('showhidden', null, PARAM_BOOL),
'qbshowtext' => optional_param('qbshowtext', null, PARAM_BOOL),
'cpage' => optional_param('cpage', null, PARAM_INT),
'qtagids' => optional_param_array('qtagids', null, PARAM_INT),
'page' => 'questions',
];
for ($i = 1; $i <= \core_question\local\bank\view::MAX_SORTS; $i++) {
$sort = optional_param("qbs$i", '', PARAM_TEXT);
if ($sort) {
$params["qbs$i"] = $sort;
} else {
break;
}
}
list($url, $contexts, $cmid, $cm, $capquiz, $pagevars) =
question_build_edit_resources('editq', $PAGE->url->out_as_local_url(false, []), $params);
parent::__construct($contexts, $url, $cm->course, $cm, $pagevars);
}

/**
* Specify the column heading
Expand Down
Loading

0 comments on commit 71e4efa

Please sign in to comment.